package com.keyancompany.zhongbaopingtai.activity;


import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.RewardAdapter;
import com.keyancompany.zhongbaopingtai.api.CollectionAPI;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.FindModel;
import com.keyancompany.zhongbaopingtai.model.ProjectTalkComment;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.weight.NoScorllerListView;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * 项目详情
 */
public class ProjectDetailActivity extends BaseActivity {


    @BindString(R.string.project_reward)
    String projectTitle;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.team_name)
    TextView title;  //项目名称

    @Bind(R.id.author)
    TextView author; //作者

    @Bind(R.id.create_time)
    TextView time; //发布时间

    @Bind(R.id.period)
    TextView period; //周期

    @Bind(R.id.moeny)
    TextView money; //金额

    @Bind(R.id.project_desc)
    TextView projectDesc;

    @Bind(R.id.project_content)
    TextView projectContent;

    @Bind(R.id.technical_request)
    TextView technicalRequest;

    @Bind(R.id.technical_request_value)
    TextView technicalRequestValue;

    @Bind(R.id.project_adjunct)
    TextView projectAdjunct;

    @Bind(R.id.project_adjunct_value)
    TextView projectAdjunctValue;

    @Bind(R.id.project_enjoy_value)
    TextView projectEnjoyValue; //应赏要求

    @Bind(R.id.detail_noscorll_listview)
    NoScorllerListView detailNoscorllListview;

    @Bind(R.id.project_detail_collection_icon)
    ImageView collectionIcon;

    private RewardAdapter mAdapter;
    private List<ProjectTalkComment> lists;

    FindModel currentModel;
    private int position;

    private String pid;

    private int currentPage = 1, total = 1;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_project_detail;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        toolbar.setTitle(projectTitle);
        setSupportActionBar(toolbar);
        initHomeAsUpEnable(toolbar);
    }

    @Override
    protected void setUpViews() {

        currentModel = (FindModel) getIntent().getSerializableExtra(Constants.ITEM);
        position = getIntent().getIntExtra("position", -1);
        String res = getIntent().getStringExtra("res");

        if (res.equals("collection")) {
            pid = currentModel.getProjectpid();
        } else if (res.equals("project")) {
            pid = currentModel.getPID_();
        } else {
            pid = TextUtils.isEmpty(currentModel.getPID_()) ? currentModel.getPid() : currentModel.getPID_();
        }

        setData(currentModel);

        TeamAPI.loadProject(pid, this, null);

    }

    public void setData(FindModel currentModel) {
        title.setText(currentModel.getTitle());
        time.setText(getString(R.string.publish_time_text, currentModel.getStartTime() == null ?
                "未知" : currentModel.getStartTime()));

        collectionIcon.setImageResource(currentModel.getCollectFlag() == 1 ?
                R.mipmap.hascollection : R.mipmap.collection);
        author.setText(getString(R.string.publish_author_text,
                TextUtils.isEmpty(currentModel.getUsername())
                        ? "匿名" : currentModel.getUsername()));
        period.setText(getString(R.string.publish_end_time_text, currentModel.getEndTime()));
        money.setText(getString(R.string.money_text, currentModel.getBudget() + ""));
        technicalRequestValue.setText(currentModel.getTcategory() == null ?
                getString(R.string.none_text) : currentModel.getTcategory().toString());
        projectEnjoyValue.setText(currentModel.getPindustrys() == null ?
                getString(R.string.none_text) : currentModel.getPindustrys().toString());

        mAdapter = new RewardAdapter(null, mContext);
        detailNoscorllListview.setAdapter(mAdapter);

        projectDesc.setText(Html.fromHtml(currentModel.getDescription()));
    }

    @OnClick(R.id.to_sign_up)//报名
    public void toSignUp(View view) {

        Bundle bundle = new Bundle();
        bundle.putString("projectId", currentModel.getPID_());
        IntentUtils.startActivity(mContext, SignUpActivity.class, bundle);
    }

    @OnClick(R.id.to_reward)//讨论
    public void toReward(View view) {
        if (lists == null || lists.isEmpty()) return;
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.ITEM, (Serializable) lists);
        bundle.putString("projectId", currentModel.getPID_());
        bundle.putInt(Constants.TOTAL, total);

        IntentUtils.startActivity(mContext, RewardActivity.class, bundle);
    }

    @OnClick(R.id.project_detail_collection_icon)//收藏
    public void collectionAbout(View view) {

//        CollectionAPI.addCollection(currentModel.getPID_(), this, getLoading(-1));
        //projectEnjoyValue.setText(Html.fromHtml());
//        IntentUtils.startActivity(mContext, RewardActivity.class);
        if (currentModel.getCollectFlag() == 1) {
            CollectionAPI.deleteCollection(currentModel.getPID_(),
                    this, getLoading(-1));
        } else {
            CollectionAPI.addCollection(currentModel.getPID_(), this, getLoading(-1));
        }
    }


    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (!base.isSuccess()) return;
        if (type == TeamAPI.LOAD_PROJECT) {
            FindModel find = JSONObject.parseObject(base.data, FindModel.class);
            setData(find);
            UserAPI.searchComment(pid, "" + currentPage, this, getLoading(R.string.watting_hint));
        } else if (type == UserAPI.SEARCH_COMMENT) {

            JSONObject jsonObject = JSONObject.parseObject(base.data);
            if (jsonObject.containsKey("pager")) {
                JSONObject pager = jsonObject.getJSONObject("pager");
                currentPage = pager.getInteger(Constants.CURRENT);
                total = pager.getInteger(Constants.TOTAL);

                lists = JSONArray.parseArray(pager.getString("list"), ProjectTalkComment.class);
            }

            if (lists == null || lists.isEmpty()) {
                return;
            }
            mAdapter.clear();
            mAdapter.addAllAfter(lists.size() > 5 ? lists.subList(0, 5) : lists);
        } else {
            if (type == CollectionAPI.ADD_COLLECTION) {
                currentModel.setCollectFlag(1);
                collectionIcon.setImageResource(R.mipmap.hascollection);
            } else if (type == CollectionAPI.DELETE_COLLECTION) {
                currentModel.setCollectFlag(0);
                collectionIcon.setImageResource(R.mipmap.collection);
            }
            EventBusEntity entity = new EventBusEntity(EventBusEntity.Type.COLLECTION);
            entity.setPosition(position);
            entity.setItem(currentModel);
            EventBus.getDefault().post(entity);
        }
    }

}
